using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Office2007Renderer;

namespace eTMS.Controls
{
    public class TabControl : System.Windows.Forms.TabControl
    {
        public TabControl()
        {
            this.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
            this.StartGradientColor = Color.FromArgb(220, 220, 255);// Color.FromArgb(150, 80, 180, 226);
            this.EndGradientColor = Color.FromArgb(200, 111, 167, 217);
            this.startgradColor = this.deactivestartColor;
            this.endgradColor = this.deactiveendColor;
            this.HoverStartColor = Color.FromArgb(100, 252, 239, 220);
            this.HoverEndColor = Color.FromArgb(200, 252, 174, 63);
        }

        public Color HoverStartColor
        {
            get
            {
                return hoverstartColor;
            }
            set
            {
                hoverstartColor = value;
            }
        }
        public Color HoverEndColor
        {
            get
            {
                return hoverendColor;
            }
            set
            {
                hoverendColor = value;
            }
        }

        private Color hoverstartColor;
        private Color hoverendColor;
        private Color deactivestartColor;
        private Color deactiveendColor;
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            if (e.State == System.Windows.Forms.DrawItemState.Selected)
            {
                this.startgradColor = this.hoverstartColor;
                this.endgradColor = this.hoverendColor;
            }
            else
            {
                this.startgradColor = this.deactivestartColor;
                this.endgradColor = this.deactiveendColor;
            }
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            RectangleF tmpRC = e.Graphics.ClipBounds;
            e.Graphics.SetClip(rect);
            DrawBackGroundItem(e.Graphics, e.Bounds);
            e.Graphics.SetClip(tmpRC);
            System.Drawing.StringFormat ftm = new System.Drawing.StringFormat();
            ftm.Alignment = System.Drawing.StringAlignment.Center;
            if (e.State == DrawItemState.Selected){
                ftm.LineAlignment = System.Drawing.StringAlignment.Center;
            }
            else
            {
                ftm.LineAlignment = System.Drawing.StringAlignment.Near;   
            }
            e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, System.Drawing.SystemBrushes.ControlText, rect, ftm);
        }
        
        private Color endgradColor;
        private Color startgradColor;
        private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
        private float gradPercent = 0.5f;
        public LinearGradientMode GradientMode
        {
            get
            {
                return gradientMode;
            }
            set
            {
                gradientMode = value;
                this.Invalidate();
            }
        }
        public float GradientPercent
        {
            get
            {
                return gradPercent;
            }
            set
            {
                gradPercent = value;
                this.Invalidate();
            }
        }
        public Color StartGradientColor
        {
            get
            {
                return deactivestartColor;
            }
            set
            {
                deactivestartColor = value;
                this.Invalidate();
            }
        }
        public Color EndGradientColor
        {
            get
            {
                return deactiveendColor;
            }
            set
            {
                deactiveendColor = value;
                this.Invalidate();
            }
        }
        private void DrawBackGroundItem(Graphics g, System.Drawing.Rectangle rect)
        {

            // Create a rectangle
            //ButtonRenderer.(g, this.ClientRectangle, System.Windows.Forms.VisualStyles.PushButtonState.Default);
            // Create a linear gradient brush
            LinearGradientBrush rgBrush =
                new LinearGradientBrush(
                rect, this.startgradColor, this.endgradColor, gradientMode
                );					// Set signma bell shape
            rgBrush.SetSigmaBellShape(gradPercent, 1.0f);
            // Fill rectangle again
            g.FillRectangle(rgBrush, rect);
            rgBrush.Dispose();
            if (this.Enabled)
            {
                Blend blend = new Blend();
                float[] factArray = { 0.0f, 0.3f, 0.8f, 1.0f };
                float[] posArray = { 0.0f, 0.4f, 0.6f, 1.0f };
                // Set Factors and Positions properties of Blend
                blend.Factors = factArray;
                blend.Positions = posArray;
                // Set smoothing mode of Graphics object
                g.SmoothingMode = SmoothingMode.AntiAlias;
                // Create path and add a rectangle
                GraphicsPath path = new GraphicsPath();
                Rectangle rc = new Rectangle(rect.X - rect.Width / 2, rect.Y - rect.Height, 2 * rect.Width, 6 * rect.Height);
                path.AddEllipse(rc);
                // Create path gradient brush
                PathGradientBrush pthBrush =
                    new PathGradientBrush(path);
                // Set Blend and FocusScales properties
                pthBrush.Blend = blend;
                pthBrush.FocusScales = new PointF(0.9f, 0.2f);
                Color[] colors = { Color.FromArgb(10, this.startgradColor) };
                // Set CenterColor and SurroundColors properties
                pthBrush.CenterColor = Color.FromArgb(220, Color.White);
                pthBrush.SurroundColors = colors;
                g.FillEllipse(pthBrush, rc);
                pthBrush.Dispose();
            }
            // Dispose of object


        }
        protected void DrawBackGround(Graphics g)
        {
            if (this.ClientRectangle.Width > 0 && this.ClientRectangle.Height > 0)
            {
                // Cannot paint a zero sized area
                Office2007ColorTable colors = new Office2007ColorTable();
                using (LinearGradientBrush backBrush = new LinearGradientBrush(ClientRectangle,
                                                                              Color.FromArgb(90, colors.ToolStripContentPanelGradientEnd),
                                                                               Color.FromArgb(90, colors.ToolStripContentPanelGradientBegin),
                                                                               90f))
                {
                    g.FillRectangle(backBrush, ClientRectangle);
                }
            }
        }
    }
}
